const state={
	createClassDialog: false,
	classId:'',
	classInfo:{},
	classroomlist:[],
	teacherlist:[]
};

const getters={
	
	isShow(state) {  //方法名随意,主要是来承载变化的createClassDialog的值
       return state.createClassDialog
   },
   getClassInfo(state){
   	return state.classInfo
   },
   getClassId(state){
   	return state.classId;
   },
   getClassroomlist(state){
   	
   	return state.classroomlist
   },
   getTeacherlist(state){
   	
   	return state.teacherlist;
   }
};

const mutations = {
	show(state) {   //自定义改变state初始值的方法，这里面的参数除了state之外还可以再传额外的参数(变量或对象);
        state.createClassDialog = true;
    },
    hide(state) {  //同上
        state.createClassDialog = false;
    },
    setClassId(state,data){
    	
    	state.classId=data;
    },
    setClassInfo(state,data){
    	
    	state.classInfo=data;
    },
    setClassroomlist(state,data){
    	
    	state.classroomlist=data;
    },
    setTeacherlist(state,data){
    	
    	state.teacherlist=data;
    }
};

const actions = {
	hidecreateClassPanel(context) {  //自定义触发mutations里函数的方法，context与store 实例具有相同方法和属性
        context.commit('hide');
    },
    showcreateClassPanel(context) {  //同上注释    	
        context.commit('show');
        let paras ={
			      className:"",
		       	  courseId:"",
		       	  totalCourseHour:"",
		       	  oneCourseHour:"",
		       	  oneCourseHourMinute:"",
		       	  recruitNum:"",
		       	  startTime:"",
		       	  endTime:"",
		       	  teacherUserId:"",
		       	  classRoomId:"",
		       	  assisUserId:"",
		       	  classStatusMode:"1"					
				}  
        context.commit('setClassInfo',paras);
    },
    
    getClassInfoById(context,param){   	
   	    let paras ={classId:param.id};     	   
   	    context.commit('setClassId',paras.classId);
   	    
   	    
   	    let classobj = {//false:可编辑  true:不可编辑
						class_name:{name:'',editor:false},//班级名称
						class_cover:{name:''},//班级封面
						course_name:{name:'',editor:false},//课程名称
						course_package_name:{name:'',editor:false},//课程包名称
						course_package:{name:[],editor:false},//所有课程包
						subject_name:{name:'',editor:true},//科目名称
						course_type_id:{name:'',editor:false},//课程类型
						course_type:{name:'',editor:true},//课程类型名称
						teach_mode:{name:'',editor:false},//授课模式
						total_course_num:{name:'',editor:true},//总课时数
						foreignTeacherType:{name:'',editor:false},//双师类型
						syllabus_end_num:{name:'',editor:false},//双师开始课节
						syllabus_start_num:{name:'',editor:false},//双师结束课节
						syllabus_num:{name:'',editor:false},//双师上课课节数
						day_startTime:{name:'',editor:false,show:false},//全日制上课时间
						day_endTime:{name:'',editor:false,show:false},//全日制结课时间
						one_hour_time:{name:'',editor:false,show:false},//普通班每次课时数
						one_mininue:{name:'',editor:false,show:false},//普通班每课时分钟
						enrollment_num:{name:'',editor:false},//计划招生数
						class_startTime:{name:'',editor:false},//开班时间
						class_endTime:{name:'',editor:false},//结班时间
						teacher:{name:''},//教师
						assister:{name:''},//助教
						classroom:{name:''},//教室
						maxCourseNum:"",//最大课时数
						class_state:{name:'1',editor:false},//班级状态
						class_type:{name:'1',editor:false,show:true},//班级类型  线上、线下
						teach_method:{name:'1',show:false},//班级类型  线上、线下,//线上课 授课方式
						normal_set:{name:'1',show:false},//班级类型  线上、线下,//线上课 常规设置
					}
   	    
   	    if(param.id!=null){//编辑班级
   	    	
   	    	let params = {classId:param.id,courseId:param.courseId};
   	        let temp = false;
   	    	api.getSyllabusCount(params).then(function(data){//判断是否已排课
   	    		
   	    		if(data.listSyllabus>0){//已有排课
   	    			temp = true;
   	    		}
   	    		
   	    		return api.getClassById(paras);
   	    	}).then(function(data){//获取班级信息  	
   	    		
	     		if(data.result=="操作成功"){
	     		
	     			let dta = data.class;
	     		   console.log(dta)
	     			//给字段赋值
	     			classobj.class_name.name = dta.className;
	     			classobj.class_name.editor = temp || dta.studentNum !=0;
	     			classobj.class_cover.name = dta.photo;
	     			classobj.course_name.name = dta.courseId||"";
	     			classobj.course_name.editor = temp;
	     			classobj.course_package_name.name = dta.coursePackageId;
	     			classobj.course_package_name.editor = temp;	     			
	     			classobj.subject_name.name = dta.subjectName;
					 classobj.course_type_id.name = dta.courseType;
					 classobj.course_type_id.editor = temp;
	     			classobj.course_type.name = getCourseType(dta.courseType);
	     			classobj.teach_mode.name = dta.courseMode||"";
	     			classobj.teach_mode.editor = temp;
	     			classobj.total_course_num.name = dta.totalCourseHour;
	     			classobj.day_startTime.name = dta.dayStartTime||"08:00";
	     			classobj.day_startTime.editor = temp;
	     			classobj.day_endTime.name = dta.dayEndTime||"21:00";
					 classobj.day_endTime.editor = temp;
					 classobj.syllabus_start_num.name=dta.syllabusStartNum;
					 classobj.syllabus_start_num.editor=temp;
					 classobj.syllabus_end_num.name=dta.syllabusEndNum;
					 classobj.syllabus_end_num.ditor=temp;
	     			classobj.one_hour_time.name = dta.oneCourseHour;
	     			classobj.one_hour_time.editor = temp;
	     			classobj.one_mininue.name = dta.oneCourseHourMinute;
	     			classobj.one_mininue.editor = temp;
	     			classobj.enrollment_num.name = dta.recruitNum;
	     			classobj.class_startTime.name = dta.startTime;
	     			classobj.class_startTime.editor = temp;
	     			classobj.class_endTime.name = dta.endTime;
	     			classobj.class_endTime.editor = temp;
	     			classobj.teacher.name = dta.teacherUserId =="0"?"":dta.teacherUserId;
	     			classobj.assister.name = dta.assisUserId =="0"?"":dta.assisUserId;
	     			classobj.classroom.name = dta.classRoomId =="0"?"":dta.classRoomId;
	     			classobj.class_state.name = dta.classStatusMode+"";
	     			classobj.class_state.editor = temp;
	     			classobj.class_type.name = dta.classTypeMode+"";
	     			classobj.class_type.editor = temp;
	     			classobj.teach_method.name = dta.classTeachingMode+"";
					 classobj.normal_set.name = dta.classTypeMode+"";
					 classobj.maxCourseNum = dta.totalCourseHour;
	     			
	     			//是否显示
	     			if(dta.courseType==1){//一对一
	     				
	     			}else if(dta.courseType==2){//全日制
	     				
	     				classobj.day_startTime.show = true;
	     				classobj.day_endTime.show = true;
	     				
	     			}else if(dta.courseType==3){//普通班
	     				
	     				classobj.one_hour_time.show = true;
	     				classobj.one_mininue.show = true;
	     				
	     			}else if(dta.courseType==4){//双师	
	     				//debugger
	     				classobj.class_type.show=false;
	     				classobj.one_hour_time.name = 1;
		     			classobj.one_hour_time.editor = true;
						 classobj.one_mininue.name = dta.oneCourseHourMinute;  
						 classobj.foreignTeacherType.name  = dta.foreignTeacherType; 
						 classobj.foreignTeacherType.editor  = true; 
		     			//classobj.total_course_num.name = dta.syllabusNum;
		     			// classobj.syllabus_end_num.name = dta.syllabusEndNum;
	     				// classobj.syllabus_end_num.editor = temp;
		     			// classobj.syllabus_start_num.editor = temp
		     			// classobj.syllabus_start_num.name = dta.syllabusStartNum;
		     			classobj.syllabus_num.editor = temp;
		     			classobj.syllabus_num.name = (dta.syllabusEndNum - dta.syllabusStartNum)+1;
	     			}
	     			classobj.one_mininue.editor = true;	
	     			//线上、线下
	     			if(dta.classTypeMode==1){//线下
	     				classobj.teach_method.show = false;
	     				classobj.normal_set.show = false;
	     			}else if(dta.classTypeMode==2){//线上
	     				classobj.teach_method.show = true;
	     				classobj.normal_set.show = true;
	     			}
	     			
	     			//是否可编辑
	     			let state = getClassState(dta.classStatusMode,dta.recruitNum);
	     			
	     			if(state==1){//待开课 未有学员报名
	     					     				
	     			}else if(state==2){//待开课 已有学员报名
	     				
	     			}else if(state==3){//上课中
	     				
	     					classobj.class_state.editor = true;	     				

	     			}
					context.commit('show');
	     			context.commit('setClassInfo',classobj);
	     		}
	     	

     		})
   	    }else{//新建班级

				
				context.commit('show');
	     		context.commit('setClassInfo',classobj);
	     		
   	    }
     	
   },
    setClassInfo(context,classInfo){
   	context.commit('setClassInfo',classInfo);
   }
};


/**
 * 获取课程类型数据字典
 */
function getCourseType(id){
	//获取数据字典
    let dicdata = tool.getItem(lsKey.dict,true);
    let coursetype = dicdata["course_type"];//班级状态数据字典  
    let coursetypeName = "";
    for(let i=0;i<coursetype.length;i++){
    	if(coursetype[i].value==id){
    		coursetypeName = coursetype[i].name;
    		break;
    	}
    }
    return coursetypeName; 
}
/**
 * 授课模式
 */
function getteachMode(id){
	
	let modename = "";
	if(id==1){
		modename = "大班";
	}else if(id==2){
		modename = "中班";
	}else if(id==3){
		modename = "小班班";
	}
	
	return modename;
}
/**
 * 班级所处状态
 */
function getClassState(classstate,studentnum){
	let status = 1;
  	if(classstate=="1"){//待开课
	
		if(studentnum<=0){//未有学员报名
			status = 1;
		}else{//已有学员报名
			status = 2;
		}  		
  	}else if(classstate=="2"){//上课中
  		status = 3;
  	}

   return status;
}

module.exports = {
	  namespaced:true,//用于在全局引用此文件里的方法时标识这一个的文件名
	  state,
	  getters,
	  actions,
	  mutations
}